//
//  YDCustomerScanController.m
//  YDTestDrive
//
//  Created by ios on 2017/2/17.
//  Copyright © 2017年 ios. All rights reserved.
//

#import "YDCustomerScanController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface YDCustomerScanController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation YDCustomerScanController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.webView.delegate = self;
    
    self.title = @"试驾二维码";
    
    self.webView.backgroundColor = kNavigationBackgroundColor;
    self.view.backgroundColor = kNavigationBackgroundColor;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.hidesBackButton = YES;
    
    UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    homeButton.frame = CGRectMake(0, 0, 16.0f, 16.0f);
    [homeButton setImage:[UIImage imageNamed:@"icon_home"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:homeButton];
    [homeButton addTarget:self action:@selector(backRootVC) forControlEvents:UIControlEventTouchUpInside];
    
    [self loadWebViewData];
}

- (void)backRootVC
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)loadWebViewData
{
    NSString *URLString = [NSString stringWithFormat:@"%@/admin-webapp/trial/testDriveQrCode?sid=%@&cid=%@&did=%@", kServerIP, getNSUser(kAPP_SID),self.customerId, self.trialId];
    NSLog(@"%@", URLString);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];
    [self.webView loadRequest:request];
    [MBProgressHUD showStatus:@"" toView:self.view];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //定义好JS要调用的方法, popToRootVC就是调用的popToRootVC方法名
    context[@"popToRootVC"] = ^() {
        NSLog(@"+++++++Begin Log+++++++");
        NSArray *args = [JSContext currentArguments];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
        
        for (JSValue *jsVal in args) {
            NSLog(@"%@", jsVal.toString);
        }
        NSLog(@"-------End Log-------");
    };
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
